package com.micai.springboot.base.bean;

import org.apache.commons.beanutils.BeanUtils;

import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/6 16:40
 * @Description: 使用org.apache.commons.beanutils进行Bean Map 转换
 */
public class BeanUtilsBeanToMap {

    /**
     * 使用org.apache.commons.beanutils进行转换
     * @param map
     * @param beanClass
     * @return
     * @throws Exception
     */
    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }
        Object obj = beanClass.newInstance();
        BeanUtils.populate(obj, map);
        return obj;
    }

    /**
     * 使用org.apache.commons.beanutils进行转换
     * @param obj
     * @return
     */
    public static Map<?, ?> objectToMap(Object obj) {
        if(obj == null) {
            return null;
        }
        return new org.apache.commons.beanutils.BeanMap(obj);
    }
}
